package org.locker.netty.endpoint;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.locker.common.core.domain.R;
import org.locker.netty.MachineService;
import org.locker.netty.dto.command.BindDevice;
import org.locker.netty.dto.resp.BindResp;
import org.locker.netty.dto.resp.VerifyCardResp;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * (machine)设备管理接口
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/machine")
@Slf4j
public class MachineEndPoint {

    private final MachineService machineService;


//    @PostMapping("/adfadfafadfsafda234aefX")
    public R<BindResp> code(
        String code,
        String deviceNo,
        String orderNo,
        String deviceGridId,
        String blueMac,
        String openCode
    ) {
        switch (code) {
            case "1" -> {
                machineService.pushByOrderNo(orderNo);
            }
            case "2" -> {
                machineService.restartDevice(deviceNo);
            }
            case "3" -> {
                machineService.setDevicePosition(deviceNo);
            }
            case "4" -> {
                machineService.controlOpenAll(deviceNo);
            }
            case "5" -> {
                machineService.synchronousGridList(deviceNo);
            }
            case "6" -> {
                machineService.batchSetGrid(deviceNo);

            }
            case "7" -> {
                machineService.deviceCommonInfoSync(deviceNo);

            }
            case "8" -> {
                boolean open = false;
                if (openCode.equals("true")) {
                    open = true;
                }
                machineService.controlBleSwitch(deviceNo, blueMac, open);
            }
            case "9" -> {
                machineService.upgradeBle(deviceNo);

            }
            case "10" -> {
                machineService.controlOpen(deviceNo, Long.valueOf(deviceGridId), "");
            }
            case "11" -> {
                machineService.orderListSync(deviceNo);
            }
            case "12" -> {
                machineService.getSimInfo(deviceNo);

            }
            case "13" -> {
                machineService.adminExec(deviceNo);

            }
            case "14" -> {
                machineService.modifyAdminAccount(deviceNo);

            }
            case "15" -> {
                machineService.uploadDeviceLog(deviceNo);

            }
            case "16" -> {
                machineService.deletedDeviceLog(deviceNo);

            }
            case "17" -> {
                machineService.deviceSingleLockStatus(deviceNo, Long.valueOf(deviceGridId));
            }
            case "18" -> {
                machineService.pushOrderFinish(orderNo,false);
            }
            case "19" -> {
                machineService.setGrid(deviceNo, Long.valueOf(deviceGridId));
            }
            case "20" -> {
                machineService.pushVersion(deviceNo);
            }
            default -> throw new IllegalStateException("Unexpected deviceNo: " + code);
        }

        return R.ok();
    }


    /**
     * 绑定设备接口
     *
     * @param time
     * @param signature
     * @param bindReq
     * @return
     */
    @PostMapping("bind")
    public R<BindResp> bind(
        @RequestHeader("timestamp") String time,
        @RequestHeader("signature") String signature,
        @RequestBody BindDevice bindReq
    ) {
        log.info("request===> bind====> time :{},signature: {} ,body:{}", time, signature, JSONUtil.parseObj(bindReq));
        BindResp resp = machineService.bindDevice(bindReq);
        log.info("response===> bind====>{}", JSONUtil.parseObj(resp));
        return R.ok(resp);
    }

    /**
     * 上传日志：
     *
     * @param time
     * @param signature
     * @param deviceNo  设备号
     * @param logType   日志类型: 默认 default
     * @param file      文件流
     * @return
     */
    @PostMapping("uploadLog")
    public R uploadLog(
        @RequestHeader("timestamp") String time,
        @RequestHeader("signature") String signature,
        @RequestParam("deviceNo") String deviceNo,
        @RequestParam("logType") String logType,
        @RequestParam("file") MultipartFile file
    ) {
        String url = machineService.uploadLog(deviceNo, logType, file);
        log.info("response===> uploadLog====>{}", url);
        return R.ok(url);
    }

    /**
     * 身份证验证
     *
     * @param time
     * @param signature
     * @param deviceGridId 格子ID
     * @param deviceNo     设备号码
     * @param orderNo      订单编号
     * @param cardNo       身份证号码
     * @return
     */
    @PostMapping("verifyCardNo")
    public R<VerifyCardResp> verifyCardNo(
        @RequestHeader("timestamp") String time,
        @RequestHeader("signature") String signature,
        @RequestParam("deviceNo") String deviceNo,
        @RequestParam("deviceGridId") Long deviceGridId,
        @RequestParam("orderNo") String orderNo,
        @RequestParam("cardNo") String cardNo
    ) {
        log.info("request===> verifyCardNo====> time :{},signature: {} ,deviceNo:{},deviceGridId:{},orderNo:{},cardNo:{}", time, signature, deviceNo, deviceGridId, orderNo, cardNo);
        VerifyCardResp ok = machineService.verifyCardNo(deviceNo, deviceGridId, orderNo, cardNo);
        log.info("response===> verifyCardNo====>{}", JSONUtil.parseObj(ok));
        return R.ok(ok);
    }


}
